package com.wzutils;

import java.math.BigDecimal;

/**
 * Created by Hu on 2016/10/31.
 */

public class BigDisMailUtils {

    public static BigDecimal turn(Object val, int i) {
        BigDecimal bd = new BigDecimal(val.toString());
        bd = bd.setScale(i, BigDecimal.ROUND_HALF_UP);//BigDecimal.ROUND_HALF_UP 是4舍5入
        return bd;
    }

    public static String turnTrim(Object val) {
        return noTrim(turn(val.toString(), 2));
    }

    public static String turnTrim(Object val, int i) {
        return noTrim(turn(val.toString(), i));
    }

    public static String noTrim(BigDecimal s) {
        String ss = s.toString();
        if (ss.indexOf(".") > 0) {
            ss = ss.replaceAll("0+?$", "");//去掉多余的0
            ss = ss.replaceAll("[.]$", "");//如最后一位是.则去掉
        }
        return ss;

    }


    /**
     * 使用java正则表达式去掉多余的.与0
     *
     * @param s
     * @return
     */
    public static String subZeroAndDot(String s) {
        if (s.indexOf(".") > 0) {
            s = s.replaceAll("0+?$", "");//去掉多余的0
            s = s.replaceAll("[.]$", "");//如最后一位是.则去掉
        }
        return s;
    }
}
